import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
import router from "@/router";
import store from './store'
import Cookies from 'js-cookie'
import 'normalize.css'
import '@/assets/css/base.css'
import dataV from '@jiaminghi/data-view'
import './mock/index.js'
import axios from 'axios';
import VueRouter from 'vue-router'
// main.js

Vue.config.productionTip = false
Vue.prototype.$axios = axios;
// Vue.use(VueCookies)
Vue.use(ElementUI);
Vue.use(dataV);

//按需引入
// Vue.use(Row)
// Vue.use(Button)

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}
//添加全局前置导航守卫
router.beforeEach((to,from,next)=>{
  //判断token存不存在
  const route = [ '/','/404','/login', '/carDelivery', '/staffManagement' , '/deptManagement', '/roleManagement','/postManagement','/passengerManagement',  '/driverManagement','/carManagement' ]
  const name = Cookies.get('name')
  //token不存在，说明当前用户是未登录，应该跳转至登录页
  if(!name && to.name !== 'login'){
    next({name:'login'})
  }else if(name && to.name === 'login'){ //token存在，说明用户登录，此时跳转至首页
    next({name:'carDelivery'})
  }else {
    if(route.indexOf(to.path)!==-1){
      next()
    }else {
      next({name:'notFound'})
    }

  }
})

new Vue({
  router,
  store,
  render: h => h(App),
  created() {
    store.commit('addMenu',router)
  }
}).$mount('#app')
